package View;

import Lexico.AnalisadorLexico;
import Sintatico.AnalisadorSintatico;
import Sintatico.ParserRD;
import Utils.Token;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.IOException;
import java.io.PushbackReader;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MainView extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextField pathArquivo;
    private JPanel mainPanel;
    private JTextArea saidaTexto;

    public MainView() {
        this.setContentPane(new JPanel(new BorderLayout()));
        this.setTitle("Compilador Pascal");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setSize(800, 600);
        this.setIconImage(new ImageIcon(System.getProperty("user.dir")
                + "/asset/favicon.ico").getImage());
        this.initComponets();
        this.setVisible(true);
        this.pack();
    }

    private void initComponets() {
        this.mainPanel = new JPanel(new BorderLayout());
        setContentPane(mainPanel);

        // Inicia escolha da pasta.
        JPanel pane = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        Insets inserts = new Insets(2, 2, 2, 2);
        c.insets = inserts;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(new JLabel("Pasta onde estão as imagens"), c);
        JPanel panelEscolha = getPanelEscolha();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        pane.add(panelEscolha, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(getButaoProcessar(), c);

        this.mainPanel.add(pane, BorderLayout.NORTH);
        this.mainPanel.add(getSaidaTexto(), BorderLayout.CENTER);

    }

    private Component getButaoProcessar() {
        JButton botao = new JButton("Processar");
        botao.addActionListener(getProcessarListener());
        return botao;
    }

    private ActionListener getProcessarListener() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    PushbackReader texto = new PushbackReader(new FileReader(
                            MainView.this.pathArquivo.getText()));
                    //C:\Users\Adriano\Documents\NetBeansProjects\compilador\teste.txt
                    AnalisadorLexico lexico = new AnalisadorLexico(texto);
                    ArrayList<Token> tokens = lexico.analisar();
                    System.out.println("******* Lista de Tokens *********");
                    imprimeEstados(tokens);
//                    AnalisadorSintatico sintatico = new AnalisadorSintatico(tokens);
//                    sintatico.analisar();
                    System.out.println("******* Analise ParserED *********");
                    ParserRD parser = new ParserRD(tokens);
                    parser.analisar();
                    //MainView.this.saidaTexto.setText();
                } catch (IOException e) {
                   
                    e.printStackTrace();
                }

            }
        };
    }

    public String imprimeEstados(ArrayList<Token> tokens) {
        String resultado = "";
        String linha = "";
        for (Token token : tokens) {
//            linha = "Token: " + token.getNome() + " Valor: " + token.getValor() 
//                + " Linha: " + token.getLinha() + " Coluna: " + token.getColuna() 
//                +  '\n';
//            resultado = resultado + linha;
            System.out.println(token);
        }
        return resultado;
    }

    private Component getSaidaTexto() {
        saidaTexto = new JTextArea();
        return saidaTexto;
    }

    private JPanel getPanelEscolha() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        pathArquivo = new JTextField();
        panel.add(pathArquivo, BorderLayout.CENTER);
        JButton escolha = new JButton(new ImageIcon(
                System.getProperty("user.dir") + "/asset/abrir_arquivo.jpg"));
        escolha.addActionListener(getEscolhaPastaListener());
        panel.add(escolha, BorderLayout.EAST);
        return panel;
    }

    private ActionListener getEscolhaPastaListener() {
        return new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setCurrentDirectory(new java.io.File("."));
                fileChooser.setDialogTitle("Escolha a pasta");
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.setCurrentDirectory(new java.io.File(pathArquivo
                        .getText()));
                if (fileChooser.showOpenDialog(MainView.this) == JFileChooser.APPROVE_OPTION) {
                    pathArquivo.setText(fileChooser.getSelectedFile()
                            .toString());
                }
            }
        };
    }

    public void showMain() {
        validate();
        repaint();
    }

}
